#import "AppDelegate.h"
#import "GeneratedPluginRegistrant.h"
#import <MapManager/MapManager.h>
#import <BMKLocationKit/BMKLocationComponent.h>

MapService *mapService;
BMKLocationManager *locationManager;

@interface AppDelegate ()<CLLocationManagerDelegate,BMKLocationAuthDelegate,BMKLocationManagerDelegate>
@property (nonatomic, strong)NSMutableDictionary *msgDic;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    _msgDic = [[NSMutableDictionary alloc] init];
    // 1.获取FlutterViewController(是应用程序的默认Controller)
    FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController;

    // 2.获取MethodChannel(方法通道)
    FlutterMethodChannel* batteryChannel = [FlutterMethodChannel
                                            methodChannelWithName:@"openLocChannel"
                                            binaryMessenger:controller.binaryMessenger];
    
    // 3.监听方法调用(会调用传入的回调函数)
    __weak typeof(self) weakSelf = self;
    [batteryChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
      // 3.1.判断是否是getBatteryInfo的调用
        if ([@"startLoc" isEqualToString:call.method]) {
            // 1.iOS中获取信息的方式
            int batteryLevel = [weakSelf getBatteryLevel];
            
            [_msgDic addEntriesFromDictionary: call.arguments];
            //
            NSString *response = [NSString stringWithFormat:@"Received message from Flutter: %@", call.arguments];
            NSLog(@"%@",response);
            NSLog(@"%@",_msgDic);
            // 2.如果没有获取到,那么返回给Flutter端一个异常
            if (batteryLevel == -1) {
                result([FlutterError errorWithCode:@"UNAVAILABLE"
                                           message:@"Battery info unavailable"
                                           details:nil]);
            } else {
                // 3.通过result将结果回调给Flutter端
                result(@(batteryLevel));
            }
        }else if ([@"stopLoc" isEqualToString:call.method]) {
            [self stopLoc];
      } else {
        // 3.2.如果调用的是getBatteryInfo的方法, 那么通过封装的另外一个方法实现回调
        result(FlutterMethodNotImplemented);
      }
    }];

   
  [GeneratedPluginRegistrant registerWithRegistry:self];
  // Override point for customization after application launch.
  return [super application:application didFinishLaunchingWithOptions:launchOptions];

}
- (int)getBatteryLevel {
    
     [[BMKLocationAuth sharedInstance] checkPermisionWithKey:@"QeL3uKONKg2Fbauh2WntGl0KWQi0LGxg" authDelegate:self];
     [[BMKLocationAuth sharedInstance]setAgreePrivacy:YES];
     locationManager = [[BMKLocationManager alloc] init];
     locationManager.delegate = self;
    
     NSMutableDictionary *err =[[ NSMutableDictionary alloc]init];
     mapService=[[MapService alloc]init];
     [mapService openServiceWithAppId:@"com.suso56.apps.SuSoApp" appSecurity:@"33459c8e-4b25-43ff-8252-d4ebee8e494e" enterpriseSenderCode:@"3329701" environment:@"debug" listener: ^( NSMutableDictionary *aError){
         if (aError) {
             [err addEntriesFromDictionary:aError];
             NSLog(@"%@",aError);
             NSLog(@"%@",[aError objectForKey:@"fail"]);
             NSLog(@"%@",[aError objectForKey:@"message"]);
         }
         NSLog(@"1111111");
         [self startLoc];
    
//
     }];
    if(!err){
        return 1;
    }else{
        NSLog(@"%@",err);
        return 0;
    }
 

}
-(void)startLoc{
    NSMutableArray *arr=[[NSMutableArray alloc]init];
//          NSMutableDictionary *dic =[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"10001",@"shippingNoteNumber",@"0000",@"serialNumber",@"320505",@"startCountrySubdivisionCode",@"320506",@"endCountrySubdivisionCode",@"31.373998384755588", @"startLongitude",@"120.74717743602213",@"startLatitude",@"31.373998384755588",@"endLongitude",@"120.74717743602213",@"endLatitude",@"虎丘区",@"startLocationText",@"吴中区",@"endLocationText",nil];
    [arr addObject:_msgDic];
    NSLog(@"arr:%@",arr);
    [mapService startLocationWithShippingNoteInfos:arr driverNameView:[_msgDic objectForKey:@"drivername"] vehicleNumberView:[_msgDic objectForKey:@"vehicleNumber"] listener:^(NSMutableDictionary *aError){
        if (aError) {
                   NSLog(@"%@",aError);
                } else {
                    NSLog(@"成功啦");
                }
    }];
}
-(void)sendLoc{
    NSMutableArray *arr=[[NSMutableArray alloc]init];

                              [arr addObject:_msgDic];
  
    [mapService sendLocationWithShippingNoteInfos:arr driverNameView:[_msgDic objectForKey:@"drivername"] vehicleNumberView:[_msgDic objectForKey:@"vehicleNumber"] listener:^(NSMutableDictionary *aError){
        if (aError) {
                   NSLog(@"%@",aError);
                } else {
                    NSLog(@"成功啦");
                }
    }];
}
-(void)stopLoc{
    [[BMKLocationAuth sharedInstance] checkPermisionWithKey:@"QeL3uKONKg2Fbauh2WntGl0KWQi0LGxg" authDelegate:self];
    [[BMKLocationAuth sharedInstance]setAgreePrivacy:YES];
    locationManager = [[BMKLocationManager alloc] init];
    locationManager.delegate = self;
   
    NSMutableDictionary *err =[[ NSMutableDictionary alloc]init];
    mapService=[[MapService alloc]init];
    [mapService openServiceWithAppId:@"com.suso56.apps.SuSoApp" appSecurity:@"33459c8e-4b25-43ff-8252-d4ebee8e494e" enterpriseSenderCode:@"3329701" environment:@"debug" listener: ^( NSMutableDictionary *aError){
        if (aError) {
            [err addEntriesFromDictionary:aError];
            NSLog(@"%@",aError);
            NSLog(@"%@",[aError objectForKey:@"fail"]);
            NSLog(@"%@",[aError objectForKey:@"message"]);
        }
        NSLog(@"1111111");
        [self startLoc];
//
    }];
    NSMutableArray *arr=[[NSMutableArray alloc]init];

                              [arr addObject:_msgDic];
  
    [mapService stopLocationWithShippingNoteInfos:arr driverNameView:[_msgDic objectForKey:@"drivername"] vehicleNumberView:[_msgDic objectForKey:@"vehicleNumber"] listener:^(NSMutableDictionary *aError){
        if (aError) {
                   NSLog(@"%@",aError);
                } else {
                    NSLog(@"成功啦");
                }
    }];
}
@end
